package com.stations.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stations.common.web.domain.request.PageDomain;
import com.stations.domain.TStation;
import com.stations.domain.TTransmitter;
import com.stations.domain.TTransmitterException;
import com.stations.mapper.TStationMapper;
import com.stations.mapper.TTransmitterExceptionMapper;
import com.stations.mapper.TTransmitterMapper;
import com.stations.service.TransmitterSendService;
import com.stations.service.TrasmiterExceptionService;
import com.stations.service.TrasmiterService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Describe: 用户服务实现类
 * Author: Ljy
 * CreateTime: 2019/10/23
 */
@Service
public class TrasmiterExceptionServiceImpl extends ServiceImpl<TTransmitterExceptionMapper, TTransmitterException> implements TrasmiterExceptionService {

    @Resource
    TTransmitterExceptionMapper tTransmitterExceptionMapper;

    @Resource
    TransmitterSendService transmitterSendService;

    @Resource(name = "TerminalRedisTemplate")
    RedisTemplate<String,Object> redisTemplate;

    @Resource
    private TTransmitterMapper tTransmitterMapper;
    @Resource
    private TrasmiterService sysTrasmiterService;
    /**
     * Describe: 获取发射机告警列表
     * Param ModelAndView
     * Return 获取发射机告警列表
     */
    @Override
    public List<TTransmitterException> getExceptions() {
        QueryWrapper<TTransmitterException> queryWrapper = new QueryWrapper<>();
        return tTransmitterExceptionMapper.selectList(queryWrapper);
    }

    /**
     * Describe: 获取发射机实时告警列表
     * Param ModelAndView
     * Return 获取发射机实时告警列表
     */
    @Override
    public List<TTransmitterException> getExceptionsRedis(String stationCode,String transCode) {
        List<TTransmitterException> lis = new ArrayList<>();
        List<TTransmitterException> lisTTransmitterException = (List)redisTemplate.opsForHash().values("TTransmitterException");
        for (TTransmitterException tTransmitterException:lisTTransmitterException) {
            String keyvalue = tTransmitterException.getStationCode() + tTransmitterException.getTransCode();
            if(keyvalue.equals(stationCode+transCode)){
                lis.add(tTransmitterException);
                //transmitterSendService.queryException(getTTransmitter(stationCode,transCode));
            }
        }
        return lis;
    }

    @Override
    public PageInfo<TTransmitterException> selectTTransmitterExceptionPage(TTransmitterException tTransmitterException, PageDomain pageDomain) {
//
//        if(tTransmitterException.getStationCode()!=null){
//            TTransmitterException[] tTransmitterExceptionNow = transmitterSendService.queryException(getTTransmitter(tTransmitterException.getStationCode(),tTransmitterException.getTransCode()),tTransmitterException.getSendTime());
//            if(tTransmitterExceptionNow.length>0){
//                tTransmitterExceptionMapper.deleteByKey(tTransmitterException.getStationCode(),tTransmitterException.getTransCode());
//            }
//            for (int i = 0; i < tTransmitterExceptionNow.length; i++) {
//                tTransmitterExceptionNow[i].setAlarmId(StrUtil.uuid());
//                tTransmitterExceptionMapper.insert(tTransmitterExceptionNow[i]);
//            }
//        }
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<TTransmitterException> data = tTransmitterExceptionMapper.selectTTransmitterExceptionList(tTransmitterException);
        return new PageInfo<>(data);
    }

    /**
     * Describe: 获取发射机
     * Param ModelAndView
     * Return 获取发射机
     */
    public TTransmitter getTTransmitter(String stationCode,String transCode) {
        QueryWrapper<TTransmitter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_code", stationCode);
        queryWrapper.eq("trans_code", transCode);
        return tTransmitterMapper.selectOne(queryWrapper);
    }

}
